<?php

class Messages extends Model {

	var $NOT_READED = 1;
	var $READED = 2;

	function Messages() {
		parent::Model();
	}

	function create( $sender_id, $receiver_id, 
		$subject, $text, $send_date, $status ) {

		$sql = 'insert into `messages`(`sender_account_id`, '
			.'`receiver_account_id`, `subject`, `text`, `send_date`, `status`)'
			.'values(?, ?, ?, ?, ?, ?)';
		$this->db->query($sql, array($sender_id, $receiver_id, 
			$subject, $text, $send_date, $status));
	}

	function fetch( $message_id ) {
		$sql = 'select * from `messages` where `message_id`=?';
		$r = $this->db->query($sql, array($message_id));
		return $r->row();
	}

	function fetch_by_receiver( $account_id ) {
		$sql = 'select * from `messages` where `receiver_account_id`=?';
		$r = $this->db->query($sql, array($account_id));
		return $r->result();
	}

	function remove( $message_id ) {
		$sql = 'delete from `messages` where `message_id`=?';
		$this->db->query($sql, array($message_id));
	}

	function update_status( $message_id, $status ) {
		$sql = 'update `messages` set `status`=? where `message_id`=?';
		$this->db->query($sql, array($status, $message_id));
	}

}


